package com.zr.planclass.controller;

import com.zr.planclass.pojo.*;
import com.zr.planclass.service.PlanClassService;
import com.zr.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin//支持跨域
public class PlanClassController {


    @Autowired
    private PlanClassService planClassService;

    @PostMapping("planCategory/add")
    public ResultVo addPlanClass(@RequestBody @Valid List<PlanClassAddVo> planClassAddVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = planClassService.add(planClassAddVo);
        return resultVo;
    }

    @GetMapping("planCategory/queryById")
    public ResultVo queryById(Integer id) {
        if (id == null) {
            return ResultBuilderVo.error(ErrorMessage.PLANCLASSUPDATEIDSINOT);
        }
        ResultVo resultVo = planClassService.queryById(id);
        return resultVo;
    }

    @PostMapping("planCategory/update")
    public ResultVo updatePlanClass(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = planClassService.update(planClassUpdateVo);
        return resultVo;
    }

    @PostMapping("planCategory/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlanClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = planClassService.updateStatus(planClassUpdateStatusVo);
        return resultVo;
    }

    @PostMapping("planCategory/queryPage")
    public ResultVo queryPage(@RequestBody PlanClassSelectVo planClassSelectVo) {
        ResultVo resultVo = planClassService.queryPage(planClassSelectVo);
        return resultVo;
    }

    @PostMapping("metadata/getPullDownList")
    public ResultVo getPullDownList() {
        Map<Object, List> map =planClassService.getPullDownList();
        return ResultBuilderVo.success(map);

    }
    /*//导入
    @PostMapping("quotation/InquiryIntroduction")
    public ResultVo InquiryIntroduction(MultipartFile file)throws Exception{
        return planClassService.importPlanClass(file);
    }*/
    //material/exportMater
    @GetMapping("material/exportMater")
    public ResultVo exportMater(HttpServletResponse response,String code , String name, Integer status)throws Exception{
        return planClassService.exportMater(response,code,name,status);
    }

}



